package com.zjh.springsecurity08password.service;


import com.zjh.springsecurity08password.mapper.UserMapper;
import com.zjh.springsecurity08password.pojo.Role;
import com.zjh.springsecurity08password.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Auther: zhaojh
 * @Date: 2025/9/4 - 09 - 04 - 23:15
 * @Description: com.zjh.securitysplit.service
 * @version: 1.0
 */
@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.getUserByName(username);
        if (ObjectUtils.isEmpty(user)) throw new UsernameNotFoundException("用户不存在");
        List<Role> rolesByUid = userMapper.getRolesByUid(user.getId());
        user.setRoles(rolesByUid);
        return user;
    }
}
